﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="index.aspx.cs" Inherits="YMImportWeb.index" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>数据导入</title>
    <link href="/YMImport/Content/css/bootstrap.min.css" rel="stylesheet" />
    <script src="/YMImport/Content/js/jquery-1.11.3.js"></script>
    <script src="/YMImport/Content/js/ajaxfileupload.js"></script>
    <%--<script src="/YMImport/Content/js/jquery.blockUI.js"></script>--%>
    <script src="/YMImport/Content/layerV3.1.1/layer/layer.js"></script>
    <link href="/YMImport/Content/layerV3.1.1/layer/theme/default/layer.css" rel="stylesheet" />
</head>
<body style="margin-top:20px;" class="form-horizontal">
    <div class="col-xs-12">
        <div class="form-group">
            <label for="file" class="col-sm-3 control-label">批量导入</label>
            <div class="col-sm-8">
                <input type="file" id="filed" name="filed" class="form-control" style="height: auto;" />
            </div>
        </div>
        <div class="form-group">
            <div class="col-sm-offset-3 col-sm-8">
                <button type="button" id="btnImport" onclick="importExcel()" class="btn btn-info" style="outline: none;">导入</button>
            </div>
        </div>
    </div>
</body>
</html>
<script type="text/javascript">
    function getFileName(o) {
        var pos = o.lastIndexOf("\\");
        return o.substring(pos + 1);
    }
    function importExcel() {
        var file = $("#filed").val();
        var fileName = getFileName(file);
        //导入前验证        
        if (file == "" || file.length == 0) {
            layer.msg("请选择文件");
            return false;
        }
        var reg = /^.*\.(?:xls|xlsx)$/i;//文件名可以带空格 
        if (!reg.test(file)) {
            layer.msg("请选择Excel文件");
            return false;
        }

        //遮罩
        //BlockPage();
        layer.msg('数据导入中，请稍候...', { time: 300000 });

        //上传文件
        $.ajaxFileUpload({
            fileElementId: 'filed',
            url: 'import.ashx',//处理程序路径
            secureuri: false,
            dataType: 'json',
            success: function (data) {
                if (data == "导入完成") {                    
                    layer.alert(data, { icon: 1 });
                } else {
                    layer.alert(data, { icon: 2, area: ['480px', '330px'] });
                }
            },
            error: function (data, status, e) {
                //layer.msg("导入异常，请检查Excel文件");
            }
        });
    }
    //function importExcel() {        
    //    var file = $("#filed").val();
    //    //导入前验证        
    //    if (file == "" || file.length == 0) {
    //        layer.msg("请选择文件！");
    //        return false;
    //    }
    //    var reg = /^.*\.(?:xls|xlsx)$/i;//文件名可以带空格 
    //    if (!reg.test(file)) {
    //        layer.msg("请选择Excel文件！");
    //        return false;
    //    }
    //    //遮罩
    //    //BlockPage();
    //    layer.msg('数据导入中，请稍候...', { time: 300000 });

    //    $.ajax({
    //        type: "post",
    //        url: "import.ashx",
    //        dataType: "text",
    //        success: function (data) {
    //            if (data == "导入完成") {
    //                layer.msg(data);
    //                setTimeout(function () {
    //                    top.location.reload();
    //                }, 1000);
    //            } else {
    //                layer.confirm(data, {
    //                    btn: ['关闭']
    //                }, function () {
    //                    top.location.reload();
    //                });
    //            }
    //        }
    //    });
    //}

    //function BlockPage() {
    //    $.blockUI({
    //        message: '<img src="/YMImport/Content/img/loading.gif" style="vertical-align:bottom;" />&nbsp;&nbsp;数据导入中...',
    //        css: { border: '1px solid #ccc', padding: '5px', left: (($(window).width() - 135) / 2 + 'px'), top: (($(window).height() - 44) / 2 + 'px'), width: '135px' }
    //    });
    //}
    //function UnBlockPage() { setTimeout(function () { $.unblockUI(); }, 600); }
</script>